package com.sunburst.demo.actor.showcases.user;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedAbstractActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;

/**
 * 用户
 */
public class UserActor extends UntypedAbstractActor {

    private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);

    private String name;
    private Long amount;

    public UserActor() {
    }

    public UserActor(String name, Long amount) {
        this.name = name;
        this.amount = amount;
    }

    public static Props create(String name, Long amount) {
        return Props.create(UserActor.class, name, amount);
    }

    @Override
    public void onReceive(Object message) throws Throwable {
        if (message instanceof String) {
            log.info("==== selfActor({}, {}), receive msg: {} ", this.name, this.amount, message);

            getSender().tell("yes, I am " + this.name + ". I have coin: " + this.amount, ActorRef.noSender());
        } else {
            log.warning("==== name:{}, amount: {}, receive msg: {} ", this.name, this.amount, message);
        }
    }

}
